package noticias;


import java.util.Collection;

import org.hibernate.Session;
import org.hibernate.Transaction;

import util.HibernateUtil;


public class NoticiaDAO {


		public void postando(Noticia noticia){
			 
			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			
			session.save(noticia);
			transaction.commit();
		}
		
		public Noticia pesquisa(int ID){
				Session session = HibernateUtil.getSessionFactory().openSession();
				Noticia post = null;
				post = (Noticia)session.createQuery("from noticia where id_noticia = :id ")
						.setInteger("id", ID).uniqueResult();
			return post;
		}
		
		@SuppressWarnings("unchecked")
		public Collection<Noticia> todasNoticias(){	
			Session session = HibernateUtil.getSessionFactory().openSession();
			String SQL = session.createQuery("from noticia").toString();
			System.out.println("SQL :: " + SQL);
			Collection<Noticia> coll = (Collection<Noticia>) session.createQuery("from noticia").list();
			
			return coll;
		}
		
		/*public void excluindo(String dono){
			
			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();

			Anuncio anun = pesquisa(Integer.parseInt(dono)); 

			session.delete(anun);
			transaction.commit();
			
		}*/
		
		/*public Collection<Anuncio> anunscat(String cat){	
			Session session = HibernateUtil.getSessionFactory().openSession();
			 Collection< Anuncio > coll = new LinkedList< Anuncio >();
			 //coll = (Collection< Anuncio >) session.createQuery("select * from Anuncio where id_categoria = :categoria").setString("categoria", "(select codigo from Categoria where desc_categoria =)" + cat).list();
			 
			 //Categoria categoria = categoriaespecifica(cat);
			 
			 //coll = (Collection< Anuncio >) session.createQuery("from Anuncio where id_categoria = :descri").setInteger("descri", categoria.getCodigo()).list();
			
			 return coll;
		}*/

		/*public Categoria categoriaespecifica(String cat){
			Session session = HibernateUtil.getSessionFactory().openSession();
			Categoria categoria = (Categoria) session.createQuery("from Categoria where desc_categoria = :categ").setString("categ", cat).uniqueResult();
			return categoria;
		}
		*/
		/*public Collection<Categoria> CatAnuncio() {
			Session session = HibernateUtil.getSessionFactory().openSession();
			Collection<Categoria> coll = (Collection<Categoria>) session.createQuery("from Categoria").list();
			
			return coll;
		}	*/
		/*public void alterando(Anuncio anun){
			Session session = HibernateUtil.getSessionFactory().openSession(); 
			Transaction transaction = session.beginTransaction();
			
			Anuncio salvo = pesquisa(anun.getId_anuncio());
			
			salvo.setCidade(anun.getCidade());
			salvo.setEstado(anun.getEstado());
			salvo.setDesc_anuncio(anun.getDesc_anuncio());
			salvo.setTitulo(anun.getTitulo());
			salvo.setValoranuncio(anun.getValoranuncio());
			//salvo.setCat(anun.getCat());
			
			session.update(salvo);
			transaction.commit();
		}
		*/

	
	
}
